<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busComment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAvatar.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busRating.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/resource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/comment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/rating.php';

class CommentController extends AbstractController {
	private $_commentBus;	
	private $_userBus;	
	private $_avatarBus;
	private $_ratingBus;
		
	public function main(array $getVars) {
		global $ubi;
		
		$this->_userBus = new UserBus();
		$this->_commentBus = new CommentBus();
		$this->_avatarBus = new AvatarBus();
		$this->_ratingBus = new RatingBus();
				
		$userID = isset($_SESSION['userid'])?$_SESSION['userid']:null;
		if ($getVars['action'] == 'load') {	
			if (!isset($getVars['page'])) {
				$getVars['page'] = 1;
			}		
			$numberOfComment = $this->_commentBus->getNumberOfComments($getVars['indicteeID'], $getVars['interactType']);
			$numberOfPages = GeneralFunctions::calculateNumberOfPages($numberOfComment, $ubi['COMMENT']['COMMENTS_PER_PAGE']);
			if ($getVars['page'] == 'last') {
				$getVars['page'] = $numberOfPages;
			}
			$comments = $this->_commentBus->getCommentsOfPage($getVars['indicteeID'], $getVars['interactType'], $getVars['page']);
			
			$ratingValues = $this->_commentBus->getRatingValues($comments, $userID);
			$users = $this->_commentBus->getUsersOfComments($comments);
			$imgs = $this->_avatarBus->getAvatarPathOfUsers($users);			
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewComments.php';
		} else if ($getVars['action'] == 'plus') {
			if ($userID != null) {
				$ck = $this->_commentBus->plus($getVars['commentID'], $userID);
				if ($ck) {
					$com = $this->_commentBus->getCommentByID($getVars['commentID']);
					$ratingValues = array();
					$ratingValues[$com->getCommentID()] = $ubi['RATING']['PLUS'];
					require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewOneComment.php';
				} 
			}
		} else if ($getVars['action'] == 'minus') {
			if ($userID != null) {				
				$ck = $this->_commentBus->minus($getVars['commentID'], $userID);
				if ($ck) {
					$com = $this->_commentBus->getCommentByID($getVars['commentID']);
					$ratingValues = array();
					$ratingValues[$com->getCommentID()] = $ubi['RATING']['MINUS'];
					require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewOneComment.php';
				}
			}
		}
	}
}
?>